import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:${USER}
 * Date:${YEAR}-${MONTH}-${DAY}
 * ${TIME}
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);

        List<List<Integer>> ret=new ArrayList<>();

        int n=nums.length;
        for(int i=0;i<n;){
            if(nums[i]>0)break;
            int left=i+1,right=n-1,tarage=-nums[i];
            while(left<right){
                int num=nums[left]+nums[right];
                if(num>tarage)right--;
                else if(num<tarage)left++;
                else{
                    ret.add(new ArrayList( Arrays.asList(nums[i],nums[left],nums[right])));
                    right--;
                    left++;
                    while(left<right&&nums[left]==nums[left-1])left++;
                    while(left<right&&nums[right]==nums[right+1])right--;
                }
            }
            i++;
            while(i<n&&nums[i]==nums[i-1])i++;
        }
        return ret;
    }
}